<?php
class Shop extends DataObject implements PermissionProvider {
    
    static $db = array(
        'Name' => 'Varchar(200)',
        'IsThals' => 'Boolean',
        'IsPremium' => 'Boolean',
        'IsDiscount' => 'Boolean',
        
        'Caption' => 'Varchar(85)',
        'Address' => 'Varchar(200)',
        'Zip' => 'Varchar(200)',
        'Town' => 'Varchar(200)',
        'Telephone' => 'Varchar(200)',
        'Fax' => 'Varchar(200)',
        'Email' => 'Varchar(200)',
        'Web' => 'Varchar(200)',
        'Description' => 'HTMLText',

        'OpenMon' => 'Boolean',
        'OpenTimeMon' => 'Varchar(3)',
        'CloseTimeMon' => 'Varchar(3)',
        'OpenTimeMonMin' => 'Varchar(2)',
        'CloseTimeMonMin' => 'Varchar(2)',
        
        'OpenTue' => 'Boolean',
        'OpenTimeTue' => 'Varchar(3)',
        'CloseTimeTue' => 'Varchar(3)',
        'OpenTimeTueMin' => 'Varchar(2)',
        'CloseTimeTueMin' => 'Varchar(2)',
        
        'OpenWed' => 'Boolean',
        'OpenTimeWed' => 'Varchar(3)',
        'CloseTimeWed' => 'Varchar(3)',
        'OpenTimeWedMin' => 'Varchar(2)',
        'CloseTimeWedMin' => 'Varchar(2)',
        
        'OpenThu' => 'Boolean',
        'OpenTimeThu' => 'Varchar(3)',
        'CloseTimeThu' => 'Varchar(3)',
        'OpenTimeThuMin' => 'Varchar(2)',
        'CloseTimeThuMin' => 'Varchar(2)',
        
        'OpenFri' => 'Boolean',
        'OpenTimeFri' => 'Varchar(3)',
        'CloseTimeFri' => 'Varchar(3)',
        'OpenTimeFriMin' => 'Varchar(2)',
        'CloseTimeFriMin' => 'Varchar(2)',
        
        'OpenSat' => 'Boolean',
        'OpenTimeSat' => 'Varchar(3)',
        'CloseTimeSat' => 'Varchar(3)',
        'OpenTimeSatMin' => 'Varchar(2)',
        'CloseTimeSatMin' => 'Varchar(2)',
        
        'OpenSun' => 'Boolean',
        'OpenTimeSun' => 'Varchar(3)',
        'CloseTimeSun' => 'Varchar(3)',
        'OpenTimeSunMin' => 'Varchar(2)',
        'CloseTimeSunMin' => 'Varchar(2)',
        
        'OpenHol' => 'Boolean',
        'OpenTimeHol' => 'Varchar(3)',
        'CloseTimeHol' => 'Varchar(3)',
        'OpenTimeHolMin' => 'Varchar(2)',
        'CloseTimeHolMin' => 'Varchar(2)',
                
        'URLSegment' => 'Varchar(255)',
        'FirstLetter' => 'Varchar(1)'
    );
    
    static $has_one = array (
        'Location' => 'Location',
        'IntroImage' => 'Shop_IntroImage'
    );
    
    static $has_many = array (
        'ImageAttachments' => 'ShopGalleryImage'
    );
    
    static $many_many = array (
        'Categories' => 'Category'
    );
    
    static $defaults = array(  
        'Name' => 'Nieuwe Winkel',
        'URLSegment' => 'nieuwe-winkel'
    );
    
    static $singular_name = 'Winkel';
    static $plural_name = 'Winkels';
    
    static $field_labels = array(
	'Name' => 'Naam',
        'Caption' => 'Intro',
        'Description' => 'Tekst',
        'Location' => 'Straat',
        'Address' => 'Adres',
        'Zip' => 'Postcode',
        'Town' => 'Woonplaats',
        'Telephone' => 'Telefoon',
        'Fax' => 'Fax',
        'Email' => 'Email',
        'Web' => 'Web',
        
        'IsThals' => 'Aangesloten bij Thals',
        'IsDiscount' => 'Aanbieder van kadocheques',
        'IsPremium' => 'PREMIUM winkel',
        
        'OpenMon' => 'Open op maandag',
        'OpenTimeMon' => 'Openingstijd uur',
        'CloseTimeMon' => 'Sluitingstijd uur',
        'OpenTimeMonMin' => 'Openingstijd minuten',
        'CloseTimeMonMin' => 'Sluitingstijd minuten',
        
        
        'OpenTue' => 'Open op dinsdag',
        'OpenTimeTue' => 'Openingstijd uur',
        'CloseTimeue' => 'Sluitingstijd uur',
        'OpenTimeTueMin' => 'Openingstijd minuten',
        'CloseTimeueMin' => 'Sluitingstijd minuten',
        
        'OpenWed' => 'Open op woensdag',
        'OpenTimeWed' => 'Openingstijd uur',
        'CloseTimeWed' => 'Sluitingstijd uur',
        'OpenTimeWedMin' => 'Openingstijd minuten',
        'CloseTimeWedMin' => 'Sluitingstijd minuten',
        
        'OpenThu' => 'Open op donderdag',
        'OpenTimeThu' => 'Openingstijd uur',
        'CloseTimeThu' => 'Sluitingstijd uur',
        'OpenTimeThuMin' => 'Openingstijd minuten',
        'CloseTimeThuMin' => 'Sluitingstijd minuten',
        
        'OpenFri' => 'Open op vrijdag',
        'OpenTimeFri' => 'Openingstijd uur',
        'CloseTimeFri' => 'Sluitingstijd uur',
        'OpenTimeFriMin' => 'Openingstijd minuten',
        'CloseTimeFriMin' => 'Sluitingstijd minuten',
        
        'OpenSat' => 'Open op zaterdag',
        'OpenTimeSat' => 'Openingstijd uur',
        'CloseTimeSat' => 'Sluitingstijd uur',
        'OpenTimeSatMin' => 'Openingstijd minuten',
        'CloseTimeSatMin' => 'Sluitingstijd minuten',
        
        'OpenSun' => 'Open op zondag',
        'OpenTimeSun' => 'Openingstijd uur',
        'CloseTimeSun' => 'Sluitingstijd uur',
        'OpenTimeSunMin' => 'Openingstijd minuten',
        'CloseTimeSunMin' => 'Sluitingstijd minuten',
        
        'OpenHol' => 'Open op feestdagen',
        'OpenTimeHol' => 'Openingstijd uur',
        'CloseTimeHol' => 'Sluitingstijd uur',
        'OpenTimeHolMin' => 'Openingstijd minuten',
        'CloseTimeHolMin' => 'Sluitingstijd minuten',
	);
    
    static $searchable_fields = array(
        'Name',
        'Location.Street',
        'Address',
        'Zip',
        'Town',
        'Telephone',
        'Email',  
        'IsThals',
        'IsPremium',
        'IsDiscount'
    );
   
    static $summary_fields = array(
        'Name' => 'Naam',
        'Location.Street' => 'Straat',
        'Town' => 'Woonplaats',
        'Web' => 'Web',
        'IsThals' => 'Aangesloten bij Thals',
        'IsDiscount' => 'Aanbieder van kadocheques',
        'IsPremium' => 'PREMIUM winkel'
    );
    
    static $indexes = array(
        "URLSegment" => true,
        "FirstLetter" => true,
        "fulltext (Name,Description)"
    );  
    
    function Link() {
        return 'winkel/details/'.$this->URLSegment;
    }
    
    function getCMSFields() {   
                                
        $fields = parent::getCMSFields();
        
        $fields->removeFieldsFromTab('Root.Main', array(
            'URLSegment', 
            'FirstLetter', 
            'Zip', 
            'Town', 
            'Telephone', 
            'Fax', 
            'Email', 
            'Web', 
            'IntroImage',
            'Caption', 
            'Description',
            'OpenMon',
            'OpenTimeMon',
            'CloseTimeMon',
            'OpenTimeMonMin',
            'CloseTimeMonMin',
            'OpenTue',
            'OpenTimeTue',
            'CloseTimeue',
            'OpenTimeTueMin',
            'CloseTimeueMin',
            'OpenWed',
            'OpenTimeWed',
            'CloseTimeWed',
            'OpenTimeWedMin',
            'CloseTimeWedMin',
            'OpenThu',
            'OpenTimeThu',
            'CloseTimeThu',
            'OpenTimeThuMin',
            'CloseTimeThuMin',
            'OpenFri',
            'OpenTimeFri',
            'CloseTimeFri',
            'OpenTimeFriMin',
            'CloseTimeFriMin',
            'OpenSat',
            'OpenTimeSat',
            'CloseTimeSat',
            'OpenTimeSatMin',
            'CloseTimeSatMin',
            'OpenSun',
            'OpenTimeSun',
            'CloseTimeSun',
            'OpenTimeSunMin',
            'CloseTimeSunMin',
            'OpenHol',
            'OpenTimeHol',
            'CloseTimeHol',
            'OpenTimeHolMin',
            'CloseTimeHolMin'
        ));
        
        $fields->removeByName('Categories');
        $fields->removeByName('ImageAttachments');
        
        $locations = DataObject::get("Location");
        $locationsmap = $locations->toDropDownMap('ID', 'Street'); 
        
        $categories = DataObject::get("Category");
        $categoriesmap = $categories->toDropDownMap('ID', 'Name'); 
        
        $timesource = array(
            '00' => '00',
            '01' => '01',
            '02' => '02',
            '03' => '03',
            '04' => '04',
            '05' => '05',
            '06' => '06',
            '07' => '07',
            '08' => '08',
            '09' => '09',
            '10' => '10',
            '11' => '11',
            '12' => '12',
            '13' => '13',
            '14' => '14',
            '15' => '15',
            '16' => '16',
            '17' => '17',
            '18' => '18',
            '19' => '19',
            '20' => '20',
            '21' => '21',
            '22' => '22',
            '23' => '23',  
            'nvt' => 'nvt'
        );
        
        $minutesource = array(
            '00' => '00',
            '15' => '15',
            '30' => '30',
            '45' => '45'
        );
        
        $fields->addFieldToTab('Root.Main', new CheckboxField('IsThals', 'Deze winkel is aangesloten bij Thals'));
        $fields->addFieldToTab('Root.Main', new CheckboxField('IsDiscount', 'Deze winkel is aanbieder van kadocheques'));
        $fields->addFieldToTab('Root.Main', new CheckboxField('IsPremium', 'Dit is een PREMIUM winkel (opslaan om Premium gedeelte zichtbaar te maken)'));
        $fields->addFieldToTab('Root.Main', new DropdownField('LocationID', 'Straat', $locationsmap));       
        $fields->addFieldToTab('Root.Main', new CheckboxSetField('Categories', 'Categorie', $categoriesmap));
        
        $fields->addFieldToTab('Root', new Tab('Openingstijden'));
            $fields->addFieldToTab('Root.Openingstijden', new CheckboxField('OpenMon', 'Open op Maandag'));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeMon', 'Openingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeMonMin', ' : ', $minutesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeMon', 'Sluitingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeMonMin', ' : ', $minutesource));
            
            $fields->addFieldToTab('Root.Openingstijden', new CheckboxField('OpenTue', 'Open op Dinsdag'));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeTue', 'Openingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeTueMin', ' : ', $minutesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeTue', 'Sluitingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeTueMin', ' : ', $minutesource));
            
            $fields->addFieldToTab('Root.Openingstijden', new CheckboxField('OpenWed', 'Open op Woensdag'));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeWed', 'Openingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeWedMin', ' : ', $minutesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeWed', 'Sluitingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeWedMin', ' : ', $minutesource));
            
            $fields->addFieldToTab('Root.Openingstijden', new CheckboxField('OpenThu', 'Open op Donderdag'));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeThu', 'Openingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeThuMin', ' : ', $minutesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeThu', 'Sluitingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeThuMin', ' : ', $minutesource));
            
            $fields->addFieldToTab('Root.Openingstijden', new CheckboxField('OpenFri', 'Open op Vrijdag'));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeFri', 'Openingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeFriMin', ' : ', $minutesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeFri', 'Sluitingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeFriMin', ' : ', $minutesource));
            
            $fields->addFieldToTab('Root.Openingstijden', new CheckboxField('OpenSat', 'Open op Zaterdag'));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeSat', 'Openingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeSatMin', ' : ', $minutesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeSat', 'Sluitingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeSatMin', ' : ', $minutesource));
            
            $fields->addFieldToTab('Root.Openingstijden', new CheckboxField('OpenSun', 'Open op Zondag'));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeSun', 'Openingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeSunMin', ' : ', $minutesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeSun', 'Sluitingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeSunMin', ' : ', $minutesource));
            
            $fields->addFieldToTab('Root.Openingstijden', new CheckboxField('OpenHol', 'Open op Feestdagen'));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeHol', 'Openingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('OpenTimeHolMin', ' : ', $minutesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeHol', 'Sluitingstijd', $timesource));
            $fields->addFieldToTab('Root.Openingstijden', new DropdownField('CloseTimeHolMin', ' : ', $minutesource));
        
        
        if($this->IsPremium) {
            $fields->addFieldToTab('Root', new Tab('Premium'));
            
            $fields->addFieldToTab('Root.Premium', new TextField('Zip', 'Postcode'));
            $fields->addFieldToTab('Root.Premium', new TextField('Town', 'Woonplaats'));
            $fields->addFieldToTab('Root.Premium', new TextField('Telephone', 'Telefoon'));
            $fields->addFieldToTab('Root.Premium', new TextField('Fax', 'Fax'));
            $fields->addFieldToTab('Root.Premium', new TextField('Email', 'Email'));
            $fields->addFieldToTab('Root.Premium', new TextField('Web', 'Web (zonder http://)'));
            $fields->addFieldToTab('Root.Premium', new ImageField('IntroImage', 'Introductie afbeelding'));
            $fields->addFieldToTab('Root.Premium', new TextField('Caption', 'Introductietekst'));
            $fields->addFieldToTab('Root.Premium', new HTMLEditorField('Description', 'Inhoud'));
            
            $fields->addFieldToTab('Root', new Tab('Afbeeldingen'));
            $fields->addFieldToTab('Root.Afbeeldingen', new ComplexTableField(
                $this,
                'ImageAttachments', 
                'ShopGalleryImage', 
                ShopGalleryImage::$field_names, 
                ShopGalleryImage::getCMSFields_forPopup(), 
                'ShopID = '.$this->ID
            )); 
        }
        
  
        return $fields; 
        
    }
    
    function onBeforeWrite() {      
        // If there is no URLSegment set, generate one from Title
        if((!$this->URLSegment || $this->URLSegment == 'nieuwe-winkel') && $this->Name != 'Nieuwe Winkel') {
            $this->URLSegment = SiteTree::generateURLSegment($this->Name);
        } else if($this->isChanged('Name')) {
            // Make sure the URLSegment is valid for use in a URL
            $this->URLSegment = SiteTree::generateURLSegment($this->Name);
        }
  
        // Ensure that this object has a non-conflicting URLSegment value.
        $count = 2;
        while($this->LookForExistingURLSegment($this->URLSegment)){
            $this->URLSegment = preg_replace('/-[0-9]+$/', null, $this->URLSegment) . '-' . $count;
            $count++;
        }
        
        $this->FirstLetter = $this->URLSegment[0];
        
        parent::onBeforeWrite();
    }
          
    //Test whether the URLSegment exists already on another Product
    function LookForExistingURLSegment($URLSegment) {
        return (DataObject::get_one('Shop', "URLSegment = '" . $URLSegment ."' AND ID != " . $this->ID));
    }
    
    public function canView($member = null) {
        return true;
    }
    
    public function canCreate($member = null) {
        if(!$member) $member = Member::currentUser();
        if(!$member) return false;
        return (
            Permission::checkMember($member, 'ADMIN')
            || Permission::checkMember($member, 'MANAGE_SHOPS')
        );
    }
    
    public function canEdit($member = null) {
        return $this->canCreate($member);
    }
 
    public function canDelete($member = null) {
        return $this->canCreate($member);
    }
    
    public function providePermissions() {
        return array(
            'MANAGE_SHOPS' => 'Beheer Winkels'  
        );
    }
    
    
}

class Shop_IntroImage extends Image {
    
    public function generateResized($gd) {
        return $gd->resize(150,122);	
    }
      
    public function canView($member = null) {
        return true;
    }
    
    public function canCreate($member = null) {
        if(!$member) $member = Member::currentUser();
        if(!$member) return false;
        return (
            Permission::checkMember($member, 'ADMIN')
            || Permission::checkMember($member, 'MANAGE_SHOPS')
        );
    }
    
    public function canEdit($member = null) {
        return $this->canCreate($member);
    }
 
    public function canDelete($member = null) {
        return $this->canCreate($member);
    }
}